
#import <defaults/defaults.h>
#import "PrefControl.h"

#define  OWNER		"ToyViewer"
#define  pcdSIZE	"pcdSIZE"
#define  pcdBRIGHT	"pcdBRIGHT"
#define  adINTERVAL	"adINTERVAL"
#define  originUL	"originUL"

@implementation PrefControl

- init
{
	const char *p;

	if (pcdBright != nil)
		return self;
	[NXApp loadNibSection:"Preference.nib" owner:self];
	p = NXGetDefaultValue(OWNER, pcdSIZE);
	pcdSizeValue = p ? atoi(p) : 1;	/* Base/4 */
	[pcdSize selectCellWithTag: pcdSizeValue];
	p = NXGetDefaultValue(OWNER, pcdBRIGHT);
	pcdBrightValue = p ? atoi(p) : 1; /* Normal */
	[pcdBright selectCellWithTag: pcdBrightValue];

	p = NXGetDefaultValue(OWNER, adINTERVAL);
	adIntervalValue = p ? atoi(p) : 2500; /* 2.5 sec */
	[adIntSlider setIntValue: adIntervalValue];
	[self changeSlider: adIntSlider];

	p = NXGetDefaultValue(OWNER, originUL);
	origSWValue = p ? (*p == 'y' || *p == 'Y') : NO;  /* UpperLower */
	[origSW selectCellWithTag: (origSWValue ? 1 : 0)];

	return self;
}

- makeKeyAndOrderFront:sender
{
	[self init];
	[panel makeKeyAndOrderFront:sender];
	return self;
}

/* Local Method */
- writeDefault: (const char *)name value:(int)val
{
	char buf[16];
	sprintf(buf, "%d", val);
	NXWriteDefault(OWNER, name, buf);
	return self;
}

- changeValue:sender
{
	int val;
	BOOL bval;

	val = [pcdSize selectedTag];
	if (val != pcdSizeValue)
		[self writeDefault: pcdSIZE value:(pcdSizeValue = val)];
	val = [pcdBright selectedTag];
	if (val != pcdBrightValue)
		[self writeDefault: pcdBRIGHT value:(pcdBrightValue = val)];
	bval = ([origSW selectedTag] != 0);
	if (bval != origSWValue) {
		origSWValue = bval;
		NXWriteDefault(OWNER, originUL, (bval ? "YES" : "NO"));
	}
	return self;
}

- pcdSize:(int *)base bright:(int *)bright
{
	*base = pcdSizeValue;
	*bright = pcdBrightValue;
	return self;
}


- changeSlider:sender
{
	int	val;
	char	buf[16];

	/* adIntervalValue (1 = 0.1 sec) */
	val = ([sender intValue] + 50) / 100;
	if (adIntervalValue == val)
		return self;
	adIntervalValue = val;
	sprintf(buf, "%d.%d", val / 10, val % 10);
	[adIntText setStringValue: buf];
	return self;
}

- (int) autoDisplayInterval
{
	static int oldvalue = 0;
	if (oldvalue != adIntervalValue) {
		oldvalue = adIntervalValue;
		[self writeDefault: adINTERVAL value: adIntervalValue * 100];
	}
	return adIntervalValue * 100;
}

- (BOOL) originUpperLeft
{
	return origSWValue;
}

@end
